﻿using _2112A_2_MES._chubowen;
using _2112A_2_MES._XiangHe;
using _2112A_2_MES.SuJiaHaos;
using _2112A_2_MES.SuJiaHaos.Sasles;
using _2112A_2_MES.SuJiaHaos.WareHouse;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test.WangWeiHao;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace _2112A_2_MES.SuJiaHao
{
    /// <summary>
    /// 销售退货服务实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "苏佳豪")]
    public class SasleService : ApplicationService, ISasleService
    {
        /// <summary>
        /// 销售退货仓储
        /// </summary>
        private readonly IRepository<Sasle, int> _sasleRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sasleRepository">销售退货仓储</param>
        public SasleService(IRepository<Sasle, int> sasleRepository)
        {
            _sasleRepository = sasleRepository;
        }

        /// <summary>
        /// 获取销售退货列表
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns>返回销售退货的对象把</returns>
        public async Task<ReturnSelect<object>> GetSasleList(int pageSize, int pageIndex)
        {
            // 获取生产领料列表
            var sasleList = await _sasleRepository.GetListAsync();

            //// 使用 WhereIf 进行条件筛选
            //var wareHouseLists = wareHouseList
            //    .WhereIf(!string.IsNullOrEmpty(warehouseCode), x => x.WarehouseCode.Contains(warehouseCode))
            //    .WhereIf(!string.IsNullOrEmpty(warehouseName), x => x.WarehouseName.Contains(warehouseName))
            //    .ToList();

            // 计算总记录数和当前页记录
            var totalCount = sasleList.Count;
            var pageList = sasleList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var result = AutoMapperExt.MapToList<Sasle>(pageList);

            //返回结果
            var data = new { TotalCount = totalCount, PageList = result };
            return new ReturnSelect<object>(true, "0", result != null && result.Count > 0 ? data : null);
        }

        /// <summary>
        /// 销售退货添加
        /// </summary>
        /// <param name="input">销售退货实体</param>
        /// <returns></returns>
        public async Task<int> GetCreateSasle(CreateSasleDTO input)
        {
            var sasles = ObjectMapper.Map<CreateSasleDTO, Sasle>(input);
            sasles.CreateBy = "崔二强";
            sasles.CreateTime = DateTime.Now;
            await _sasleRepository.InsertAsync(sasles);
            return sasles != null ? 1 : 0;
        }

        /// <summary>
        /// 销售退货信息删除
        /// </summary>
        /// <param name="id">条件编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DeleteSasle(int id)
        {
            var sasleDel = await _sasleRepository.GetAsync(id);
            await _sasleRepository.DeleteAsync(sasleDel);
            return sasleDel != null ? 1 : 0;
        }

        /// <summary>
        /// 销售退货反填
        /// </summary>
        /// <param name="id">条件编号</param>
        /// <returns>返回客户信息实体</returns>
        public async Task<Sasle> GetSasle(int id)
        {
            var sasles = await _sasleRepository.FirstOrDefaultAsync(x => x.Id == id);
            return sasles;
        }

        /// <summary>
        /// 销售退货信息修改
        /// </summary>
        /// <param name="input">仓库实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateSasle(CreateSasleDTO input)
        {
            var sasles = ObjectMapper.Map<CreateSasleDTO, Sasle>(input);
            sasles.UpdateBy = "崔大强";
            sasles.UpdateTime = DateTime.Now;
            await _sasleRepository.UpdateAsync(sasles);
            return sasles != null ? 1 : 0;
        }
    }
}
